import { useState, PropsWithChildren } from 'react';

const useConstant = (
  initState: PropsWithChildren<{
    [props: string]: number | boolean | string | undefined | null;
  }>,
) => {
  const [state, setState] = useState<any>(initState);

  return [
    state,
    (key: string, val: any) => {
      // 如果key值 onRefresh, 传入一个对象
      if (key === 'onRefresh') {
        setState({ ...state, ...val });
        return;
      }

      const target: any = { ...state };
      target[key] = val;
      setState(target);
    },
  ];
};

export default useConstant;
